import Vue from 'vue'
import Router from 'vue-router'
import Main  from './views/Main.vue'
import Fupin from './views/Main/Fupin.vue'
import Pinkunhu from './views/Main/Pinkunhu.vue'
import Yonghu from './views/Main/Yonghu.vue'
import Zhiyuanze from './views/Main/Zhiyuanze.vue'
import Liuyanban from './views/Main/Liuyanban.vue'
import Home from './views/Home.vue'
import Index from './views/home/Index.vue'
import Lunbotu from './views/Main/Lunbotu.vue'
import Shoucang from './views/Main/Shoucang.vue'
import Login from './views/Login.vue'
import Pinkun from './views/home/Pinkun.vue'
import Alleviation from './views/home/Alleviation.vue'
import Collection from './views/home/Collection.vue'
import Msgboard from './views/home/Msgboard.vue'
import User from './views/home/User.vue'
import Volunteer from './views/home/Volunteer.vue'
import PinkunInfo from './views/home/PinkunInfo.vue'
import AlleviationInfo from './views/home/AlleviationInfo'
import VolunteerInfo from './views/home/VolunteerInfo.vue'
import VolunteerInfoMessage from './views/home/VolunteerInfoMessage.vue'
Vue.use(Router)

 var router =   new Router({

  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/home ',
      name: 'home',
      component: Home,
      redirect:'/index',
      children:[
        {
          path:'/index',
          component:Index
        },
        {
          path:'/PinkunView',
          component:Pinkun
        },
        {
          path:'/AlleviationView',
          component:Alleviation
        },
        {
          path:'/CollectionView',
          component:Collection
        },
        {
          path:'/MsgboardView',
          component:Msgboard 
        },
        {
          path:'/UserView',
          component:User
        },
        {
          path:'/VolunteerView',
          component:Volunteer
        },
        {
          path:'/PinkunInfo/:id',
          component:PinkunInfo
        },
        {
          path:'/AlleviationInfo/:id',
          component:AlleviationInfo
        },
        {
          path:'/VolunteerInfo/:id',
          component:VolunteerInfo
        },
        {
          path:'/VolunteerInfoMessage',
          component:VolunteerInfoMessage
        }
      ]
    },
    {
      path: '/main',
      name: 'main',
      component: Main,
      children:[
        {
          path: '/fupin',
          name: 'fupin',
          component: Fupin
        },
        {
          path: '/Pinkunhu',
          name: 'Pinkunhu',
          component: Pinkunhu
        },
        {
          path: '/Yonghu',
          name: 'Yonghu',
          component: Yonghu
        },
        {
          path: '/Zhiyuanze',
          name: 'Zhiyuanze',
          component: Zhiyuanze
        },
        {
          path: '/Liuyanban',
          name: 'Liuyanban',
          component: Liuyanban
        },
        {
          path: '/Lunbotu',
          name: 'Lunbotu',
          component: Lunbotu
        },
        {
          path: '/Shoucang',
          name: 'Shoucang',
          component: Shoucang
        }
      ]
    },
  ]
})
// router.beforeEach((to,from,next) =>{
//   window.console.log(`路由守卫触发，拦截${to.path}`)
//   if(to.path == '/login')return next()
//   const tonkenStr = sessionStorage.getItem('token')
//   if( ! tonkenStr) return next('/login')
//   next()
// })

router.beforeEach((to, from, next) => {
  // to 将要访问的路径  from 从哪个路径跳转过来  next 一个函数，表示放行
  if (to.path == '/login') return next()
  // 获取token
  const tokenStr = sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router